package com.zmm.myyimixian.ui.activity.address;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.zmm.myyimixian.MyApp;
import com.zmm.myyimixian.R;
import com.zmm.myyimixian.base.BaseActivity;
import com.zmm.myyimixian.db.Address;
import com.zmm.myyimixian.db.DataDao;
import com.zmm.myyimixian.util.TextUtils;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.v3.listener.SaveListener;

public class NewAddressActivity extends BaseActivity {


    @BindView(R.id.new_address_toolbar)
    Toolbar newAddressToolbar;//toolbar
    @BindView(R.id.new_address_name)
    EditText newAddressName;//名字
    @BindView(R.id.new_address_sex_nv)
    RadioButton newAddressSexNv;//性别女
    @BindView(R.id.new_address_sex_nan)
    RadioButton newAddressSexNan;//性别男
    @BindView(R.id.new_address_phone)
    EditText newAddressPhone;//手机号
    @BindView(R.id.new_address_province)
    TextView newAddressProvince;//省份
    @BindView(R.id.new_address_city)
    TextView newAddressCity;//城市
    @BindView(R.id.new_address_district)
    TextView newAddressDistrict;//区县
    @BindView(R.id.new_address_detailed)
    EditText newAddressDetailed;//详细地址
    @BindView(R.id.new_address_building)
    EditText newAddressBuilding;//楼号
    @BindView(R.id.new_address_tag)
    EditText newAddressTag;//标签
    private Address address;//数据表对象
    private String sex = "女士";

    @Override
    protected void initView() {
        address = new Address();
        address.setUserSex(sex);
        //绑定定位数据
        newAddressProvince.setText(MyApp.getProvince());
        newAddressCity.setText(MyApp.getCity());
        newAddressDistrict.setText(MyApp.getDistrict());


        //返回
        newAddressToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_new_address;
    }


    @OnClick({R.id.new_address_save, R.id.new_address_sex_nv, R.id.new_address_sex_nan, R.id.new_address_layout, R.id.new_address_down})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.new_address_save://保存按钮
                saveAddress();//保存收货地址的方法
                break;
            case R.id.new_address_sex_nv://女
                sex = newAddressSexNv.getText().toString();
                break;
            case R.id.new_address_sex_nan://男
                sex = newAddressSexNan.getText().toString();
                break;
            case R.id.new_address_layout://跳转到选择地址
                startActivityForResult(new Intent(this,WheelActivity.class),1);
                break;
            case R.id.new_address_down://弹出选择标签
                showAlertDialog();
                break;

        }
    }
    //显示alertDialog
    private void showAlertDialog() {
        final String[] arrayFruit = new String[] { "单位", "家", "学校", "其他" };
         new AlertDialog.Builder(this).
                setTitle("选择标签").
                 setItems(arrayFruit, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        newAddressTag.setText(arrayFruit[which]);
                    }
                }).setCancelable(true).show();
    }

    private void saveAddress() {
        //收货人姓名返回
        if ("".equals( newAddressName.getText().toString().trim())) {
            Toast.makeText(this, "收货人姓名为空", Toast.LENGTH_SHORT).show();
            return;
        }
        //手机号为空返回
        if ("".equals( newAddressPhone.getText().toString().trim())) {
            Toast.makeText(this, "手机号为空", Toast.LENGTH_SHORT).show();
            return;
        }
        //详细地址为空返回
        if ("".equals( newAddressDetailed.getText().toString().trim())) {
            Toast.makeText(this, "详细地址为空", Toast.LENGTH_SHORT).show();
            return;
        }
        //楼号为空返回
        if ("".equals( newAddressBuilding.getText().toString().trim())) {
            Toast.makeText(this, "请输入门牌号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!TextUtils.isMobileNO( newAddressPhone.getText().toString().trim())) {
            Toast.makeText(this, "手机号格式错误", Toast.LENGTH_SHORT).show();
            return;
        }
        //添加数据
        address.setUserName(newAddressName.getText().toString().trim());
        address.setUserSex(sex);
        address.setUserProvince(newAddressProvince.getText().toString());
        address.setUserCity(newAddressCity.getText().toString());
        address.setUserDistrict(newAddressDistrict.getText().toString());
        address.setUserDetailedAddress(newAddressDetailed.getText().toString().trim());
        address.setUserBuilding(newAddressBuilding.getText().toString());
        address.setUserTag(newAddressTag.getText().toString());
        address.setUserPhone(newAddressPhone.getText().toString());
        addAddress();
    }

    private void addAddress() {
        DataDao.addAddress(address, new SaveListener() {
            @Override
            public void onSuccess() {
                Toast.makeText(NewAddressActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                finish();
            }

            @Override
            public void onFailure(int i, String s) {
                Toast.makeText(NewAddressActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
            }
        });
    }

    //接受回传的地址
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            newAddressProvince.setText(data.getStringExtra("province"));
            newAddressCity.setText(data.getStringExtra("city"));
            newAddressDistrict.setText(data.getStringExtra("district"));

        }
    }
}
